package com.cqeec.myblog.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cqeec.myblog.model.Photo;
import com.cqeec.myblog.service.PhotoService;
import com.cqeec.myblog.service.PhotoServiceImpl;

@WebServlet("/album/photo/view.html")
public class AlbumPhotoServlet extends HttpServlet {
	private static final long serialVersionUID = -7199888759037181852L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String albumId = req.getParameter("id");
		PhotoService photoService = new PhotoServiceImpl();
		StringBuffer sbPhotoInfo = new StringBuffer();
		try {
			List<Photo> photoList = photoService.getAlbumPhoto(albumId);
			if (photoList.size() > 0) {
				sbPhotoInfo.append("[");
				for (Photo p : photoList) {
					sbPhotoInfo.append("{\"name\":\"" + p.getAlbum() + "/" + p.getFile() + "\",\"caption\":\""
							+ p.getName() + "\"},");
				}
				sbPhotoInfo.deleteCharAt(sbPhotoInfo.length() - 1);
				sbPhotoInfo.append("]");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		resp.setCharacterEncoding("UTF-8");
		PrintWriter out = resp.getWriter();
		out.write(sbPhotoInfo.toString());
	}

}
